package com.vcredit.creditcat.manager;

import com.alibaba.dubbo.config.annotation.Service;
import com.vcredit.creditcat.api.ICatCardManager;
import com.vcredit.creditcat.dto.catCard.*;
import com.vcredit.creditcat.enumtype.AuthItemEnum;
import com.vcredit.creditcat.model.*;
import com.vcredit.creditcat.service.AuthProccessService;
import com.vcredit.creditcat.service.CustService;
import com.vcredit.creditcat.service.PrivilegeCatService;
import com.vcredit.creditcat.service.catfood.CatfoodService;
import com.vcredit.creditcat.service.privilegeCat.CatCardService;
import com.vcredit.framework.annotation.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liaoshengjun on 2017/12/12.
 */
@Service(protocol = { "protocol1" }, version = "1.0")
public class CatCardManager implements ICatCardManager {
	private static final String CARD_NO="0000000000000000";
	private static final String OPEN_VIP_URL="app://creditaccount";
	@Autowired
	private CatCardService catCardService;
	@Autowired
	private CatfoodService catfoodService;
	@Autowired
	private PrivilegeCatService privilegeCatService;
	@Autowired
	private AuthProccessService authProccessService;
	@Autowired
	private CustService custService;

	@Value("${project.serviceUrl}")
	private String projectServiceUrl;

	/**
	 * 我的首页卡片展示数据封装
	 * @param cust
	 * @return
	 */
	@Override
	@Log
	public MyIndexCatCardDto myIndex(Cust cust){
		MyIndexCatCardDto myIndexCatCardDto=new MyIndexCatCardDto();
		CatCardInfoDto CatCardInfoDto=getCatCardInfo(cust);
		Long integral=catfoodService.queryCustRemainCatfood(cust);
		myIndexCatCardDto.setCardInfo(CatCardInfoDto);
		myIndexCatCardDto.setIntegral(integral);
		return myIndexCatCardDto;
	}

	/**
	 * 获取喵卡特权
	 * @param cust
	 * @return
	 */
	@Override
	@Log
	public CatCardDto getCatCardPrivilege(Cust cust){
		CatCardDto catCardDto=new CatCardDto();
		Integer cardLevel;
		Long experience;
		Long integral;
		CustCatfood custCatfood=catfoodService.queryCustCatfood(cust);
		if(custCatfood==null){
			cardLevel=0;
			experience=0L;
			integral=0L;
		}else{
			CatCard catCard=this.getCurrentCatCard(cust);
			if(catCard!=null){
				cardLevel=catCard.getCardType();
			}else{
				cardLevel=0;
			}
			experience=custCatfood.getTotalCatfood();
			integral=custCatfood.getRemainCatfood();
		}
		List<CatCardGradeDto> cardGradeList=getCatCardGradeDtos(cust,cardLevel,experience);
		List<CatCardValuesDto> privilegeCats=getPrivilegeCats(cardLevel);
		catCardDto.setCardGradeList(cardGradeList);
		catCardDto.setCardValue(privilegeCats);
		catCardDto.setExperience(experience);
		catCardDto.setIntegral(integral);
		return catCardDto;
	}

	/**
	 * 获取喵卡滑动数据列表
	 * @param cust
	 * @param cardLevel
	 * @param experience
	 * @return
	 */
	@Log
	public List<CatCardGradeDto> getCatCardGradeDtos(Cust cust,Integer cardLevel,Long experience){
		List<CatCardGradeDto> cardGradeList=new ArrayList<>();
		List<CatCard> catCardList=catCardService.findAll();
		for(CatCard catCard:catCardList){
			CatCardGradeDto catCardGradeDto=new CatCardGradeDto();
			catCardGradeDto.setCardName(catCard.getCardName());
			catCardGradeDto.setCardType(catCard.getCardType());
			if(cardLevel>=catCardGradeDto.getCardType()){
				CustCatfood custCatfood=catfoodService.queryCustCatfood(cust);
				String id=custCatfood.getId().toString();
				catCardGradeDto.setCardNo(CARD_NO.substring(0,16-id.length())+id);
				catCardGradeDto.setCardStatus(1);
				catCardGradeDto.setRealName(cust.getCustName());
				catCardGradeDto.setIntervalDesc("");
				catCardGradeDto.setUpgradeText("");
				catCardGradeDto.setUpgradeUrl("");
			}else{
				catCardGradeDto.setCardStatus(0);
				catCardGradeDto.setUpgradeUrl(catCard.getCardLink());
				Long catfoodLess=catCard.getCardCondition()-experience;
				if(catCard.getVipLevel()>0){
					catCardGradeDto.setIntervalDesc("<font color='#FFFFFF' style=\"font-size: 13px\">解锁全部特权猫</font>");
					catCardGradeDto.setConditionDesc("<font color='#FFFFFF' style=\"font-size: 13px\">开通会员即可升级</font>"+"<font color='#E0BF96'> 黑卡</font>");
					catCardGradeDto.setUpgradeText(TextConstant.OPEN_VIP_TXT);
				}else{
					catCardGradeDto.setIntervalDesc("<font color='#FFFFFF' style=\"font-size: 13px\">"+TextConstant.CATFOOD_LESS+"</font>"+"<font color='#AF8345'>"+catfoodLess+TextConstant.FEN+"</font>");
					catCardGradeDto.setConditionDesc("<font color='#FFFFFF' style=\"font-size: 13px\">"+TextConstant.CATFOOD_CONDITION+"</font>"+"<font color='#AF8345'>"+catCard.getCardCondition()+TextConstant.FEN+"</font>");
					catCardGradeDto.setUpgradeText(TextConstant.NORMAL_CARD_TEXT);
				}
				if(!checkVerifyFacePair(cust)){
					catCardGradeDto.setUpgradeUrl("app://identityAuth");
				}
			}
			cardGradeList.add(catCardGradeDto);
		}
		return cardGradeList;
	}

	/**
	 * 获取特权猫列表
	 * @param cardLevel
	 * @return
	 */
	@Log
	public List<CatCardValuesDto> getPrivilegeCats(Integer cardLevel){
		List<CatCardValuesDto> CatCardValuesDtoList= new ArrayList<>();
		List<PrivilegeCat> privilegeCatList=privilegeCatService.findAll();
		for(PrivilegeCat privilegeCat:privilegeCatList){
			if(!privilegeCat.getStatus()){
				continue;
			}
			CatCardValuesDto catCardValuesDto=new CatCardValuesDto();
			catCardValuesDto.setPrivilegeId(privilegeCat.getId().toString());
			catCardValuesDto.setPrivilegeName(privilegeCat.getName());
			catCardValuesDto.setValueDesc(privilegeCat.getTitle());
			catCardValuesDto.setCatCardType("LV"+privilegeCat.getCatCardType());
			if(cardLevel>=privilegeCat.getCatCardType()){
				catCardValuesDto.setButtonText(privilegeCat.getBtnText());
				catCardValuesDto.setPrivilegeIcon(privilegeCat.getOpenIcon());
				catCardValuesDto.setPrivilegeUrl(privilegeCat.getJumpUrl());
				catCardValuesDto.setActiveStatus(1);
			}else{
				catCardValuesDto.setButtonText(privilegeCat.getBtnTextDef());
				catCardValuesDto.setPrivilegeIcon(privilegeCat.getCloseIcon());
				catCardValuesDto.setPrivilegeUrl("");
				catCardValuesDto.setActiveStatus(0);
			}
			CatCardValuesDtoList.add(catCardValuesDto);
		}
		return CatCardValuesDtoList;
	}

	/**
	 * 获取用户当前卡信息
	 * @param cust
	 * @return
	 */
	@Override
	@Log
	public CatCardInfoDto getCatCardInfo(Cust cust){
		CatCardInfoDto cardInfoDto=new CatCardInfoDto();
		CatCard catCard=this.getCurrentCatCard(cust);
		if(cust!=null){
			cardInfoDto.setRealName(cust.getCustName());
		}
		if(catCard==null){
			cardInfoDto.setCardType(0);
			cardInfoDto.setVipIcon(projectServiceUrl+"transferImages/image_lv0.png");
			cardInfoDto.setVipDesc(TextConstant.OPEN_VIP_TXT);
			cardInfoDto.setVipLink(OPEN_VIP_URL);
		}else{
			CustCatfood custCatfood=catfoodService.queryCustCatfood(cust);
			String id=custCatfood.getId().toString();
			cardInfoDto.setCardType(catCard.getCardType());
			cardInfoDto.setCardName(catCard.getCardName());
			cardInfoDto.setCardNo(CARD_NO.substring(0,16-id.length())+id);
			if(catCard.getVipLevel()>0){
				cardInfoDto.setVipIcon(projectServiceUrl+"transferImages/image_vip.png");
				cardInfoDto.setVipDesc(TextConstant.JIESUO+"33"+TextConstant.TEQUAN);
				cardInfoDto.setVipLink("app://creditaccount");
			}else{
				cardInfoDto.setVipIcon(catCard.getCardIcon());
				cardInfoDto.setVipDesc(TextConstant.OPEN_VIP_TXT);
				cardInfoDto.setVipLink(OPEN_VIP_URL);
			}
		}
		return cardInfoDto;
	}

	/**
	 * 获取用户当前喵卡对象
	 * @param cust
	 * @return
	 */
	@Log
	public CatCard getCurrentCatCard(Cust cust){
		if(cust==null){
			return null;
		}
		CatCard result=null;
		Integer vipLevel=0;
		List<CatCard> catCardList=catCardService.findAll();
		if(vipLevel>0){
			for(CatCard catCard:catCardList){
				if(vipLevel.equals(catCard.getVipLevel())){
					result=catCard;
					break;
				}
			}
		}else{
			CustCatfood custCatfood=catfoodService.queryCustCatfood(cust);
			if(custCatfood==null){
				return null;
			}
			Long totalCatfood=custCatfood.getTotalCatfood();
			for(int i=catCardList.size()-1;i>=0;i--){
				if(catCardList.get(i).getVipLevel().equals(0) && totalCatfood >= catCardList.get(i).getCardCondition()){
					result=catCardList.get(i);
					break;
				}
			}
		}
		return result;
	}

	/**
	 * 判断是否做过高级实名
	 * @param cust
	 * @return
	 */
	public boolean checkVerifyFacePair(Cust cust){
		if(cust==null){
			return false;
		}
		boolean result=false;
		AuthProccess authProccess = authProccessService.findByProcessId(cust.getLastProccessId());
		if((authProccess.getSuccessCode() & AuthItemEnum.VERIFY_FACE_PAIR.getCode())== AuthItemEnum.VERIFY_FACE_PAIR.getCode()){
			result=true;
		}
		return result;
	}
}
